<?php

namespace MUD;

class DBInterface{
    public $result;
    public $rows;
    public $error;
    public $errno;
    protected $fd;
    protected $host;
    protected $username;
    protected $password;
    protected $database;
    protected $prefix;
    
    public function __construct($host, $user, $pass, $db, $prefix=NULL){
        $this->host=$host;
        $this->username=$user;
        $this->password=$pass;
        $this->database=$db;
        if(!is_null($this->prefix) && $this->prefix != ''){
            $this->prefix=$prefix;
        }
    }
    
    public function connect(){
        if(is_resource($this->fd)){
            return true;
        }
        $this->fd = mysqli_connect($this->host, $this->username, $this->password);
        if(!$this->fd){
            $this->error=mysqli_error($this->fd);
            $this->errno=mysqli_errno($this->fd);
            \Sock\SocketServer::cLog("Unable to connect to database server:\n".$this->error, $this->errno);
            return false;
        }else{
            if(@mysqli_select_db($this->fd, $this->database)){
                return true;
            }else{
                $this->error=mysqli_error($this->fd);
                $this->errno=mysqli_errno($this->fd);
                \Sock\SocketServer::cLog("Unable to find database on host:\n".$this->error, $this->errno);
                return false;
            }
        }
    }
    
    public function select($what,$table,$where,$limit='',$order=''){
        $sql="SELECT ".$what." FROM ".$this->prefix.$table;
        if($where){
            $sql.=" WHERE ".$where;
        }
        if($limit){
            $sql.= " LIMIT ".$limit;
        }
        if($order){
            $sql .= " ORDER BY ".$order;
        }
        return $this->query($sql,true);
    }
    
    public function update($table, $data, $where, $limit=''){
        $sql="UPDATE ".$this->prefix.$table." SET";
        foreach($data as $field => $value){
            $sql.=' '.$field."='".$value."',";
        }
        $sql=substr($sql,0,strlen($sql)-1);
        if($where){
            $sql.= " WHERE ".$where;
        }
        if($limit){
            $sql.= " LIMIT ".$limit;
        }
        return $this->query($sql,false);
    }
    
    public function delete($table, $where, $limit=''){
        $sql="DELETE FROM ".$this->prefix.$table;
        if($where){
            $sql.= " WHERE ".$where;
        }
        if($limit){
            $sql.= " LIMIT ".$limit;
        }
        return $this->query($sql, false);
    }
    
    public function insert($table, $data){
        $fields=array_keys($data);
        $sql="INSERT INTO ".$this->prefix.$table."(";
        foreach($fields as $field){
            $field='`'.$field.'`';
        }
        $fields=implode(',',$fields);
        $sql.=$fields.") VALUES(";
        foreach($data as $field => $value){
            $sql.="'".$value."',";
        }
        $sql=substr($sql, 0, strlen($sql)-1);
        $sql.=")";
        return $this->query($sql, false);
    }
    
    public function query($sql, $expectData=true){
        $result = @mysqli_query($this->fd, $sql);
        if(!$result){
            $this->error=mysqli_error($this->fd);
            $this->errno=mysqli_errno($this->fd);
            \Sock\SocketServer::cLog("Unable to execute SQL query:\n".$this->error, $this->errno);
            return false;
        }else{
            if($expectData){
                //data expected from result
                $this->rows=  mysqli_num_rows($result);
                $this->result=array();
                if($this->rows < 1){
                    return true;
                }else{
                    $x=0;
                    while(($row = mysqli_fetch_assoc($result)) && $x < $this->rows){
                        $this->result[]=$row;
                    }
                    return true;
                }
            }
            return true;
        }
    }
    
    public function close(){
        if(is_resource($this->fd)){
            mysqli_close($this->fd);
        }
    }
    
    public function __destruct() {
        $this->close();
    }
}